﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ES_Project.Model;
using System.Data;
using System.Data.SqlClient;

using System.Collections;
using System.Web.Security;

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;

using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace ES_Project.Pages
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        string StrConn = WebConfigurationManager.ConnectionStrings["baseessystem"].ToString();
        MySqlCommand objCmd = new MySqlCommand(); 
        protected void Page_Load(object sender, EventArgs e)
        {
            DataFamily();
            
          
        }
        void DataFamily()
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            String strSQL;
            strSQL = "SELECT * FROM family_mast  ";
            MySqlDataAdapter da = new MySqlDataAdapter(strSQL, Conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "family_mast");
            GridView1.DataSource = ds.Tables["family_mast"];
            GridView1.DataBind();
        }
        private void clear()
        {
            TextBox4.Text = "";
            TextBox5.Text = "";
            family_id.Text = "";
            TextBox3.Text = "";
            TextBox6.Text = "";
            TextBox7.Text = "";
            TextBox8.Text = "";
            //TextBox13.Text = "";
            TextArea2.Value = "";
            TextBox10.Text = "";
            TextBox11.Text = "";
            TextBox12.Text = "";
            TextBox5.Text = "";
            TextBox14.Text = "";
            TextBox15.Text = "";
            TextBox16.Text = "";
            
          
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            using (essystemEntities context = new essystemEntities())
            {
                Button button = (Button)sender;
                switch (button.ID)
                {
                    case "Button3":
                       
                            List<family_mast> fm = context.family_mast.Where("it.[family_code]='" + TextBox4.Text + "' ").ToList();
                            if(fm.Count == 0)
                            {
                            family_mast familymast = new family_mast();
                            familymast.family_code = TextBox4.Text;
                            familymast.family_name = TextBox5.Text;
                            familymast.father_name = TextBox6.Text;
                            familymast.mother_name = TextBox10.Text;
                            familymast.email_father = TextBox8.Text;
                            familymast.phone_father = TextBox7.Text;
                            familymast.email_mother = TextBox12.Text;
                            familymast.phone_mother = TextBox11.Text;
                            familymast.receipt_name = TextBox3.Text;
                            //familymast.address = TextBox13.Text;
                            familymast.address = TextArea2.Value;
                            familymast.parent_name = TextBox14.Text;
                            familymast.phone_parent = TextBox15.Text;
                            familymast.email_parent = TextBox16.Text;
                            context.family_mast.AddObject(familymast);
                            if (context.SaveChanges() > 0)
                            {

                                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert1", "alert('บันทึกเรียบร้อยแล้ว')", true);
                                GridView1.DataBind();
                                GridView1.SelectedIndex = -1;
                                clear();
                                DataFamily();
                            }
                        }
                        else
                        {
                            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ReturnMessini9", "alert('รหัสครอบครัวซ้ำ');", true);
                           
                        }
                        break;
                    case "Button7":
                           
                      
                          
                        List<family_mast> fmast = context.family_mast.Where("it.[family_id]=" + family_id.Text + " ").ToList();
                        if (fmast.Count() == 1)
                        {
                            
                                family_mast fmastEdit = context.family_mast.Where("it.[family_id]=" + family_id.Text + "").First();
                                if (fmastEdit != null)
                                {
                                   
                                    fmastEdit.family_code = TextBox4.Text;
                                    fmastEdit.family_name = TextBox5.Text;
                                    fmastEdit.father_name = TextBox6.Text;
                                    fmastEdit.mother_name = TextBox10.Text;
                                    fmastEdit.email_father = TextBox8.Text;
                                    fmastEdit.phone_father = TextBox7.Text;
                                    fmastEdit.email_mother = TextBox12.Text;
                                    fmastEdit.phone_mother = TextBox11.Text;
                                    fmastEdit.receipt_name = TextBox3.Text;
                                    //fmastEdit.address = TextBox13.Text;
                                    fmastEdit.address = TextArea2.Value;
                                    fmastEdit.parent_name = TextBox14.Text;
                                    fmastEdit.phone_parent = TextBox15.Text;
                                    fmastEdit.email_parent = TextBox16.Text;

                                    if (context.SaveChanges() > 0)
                                    {

                                        ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert2", "alert('แก้ไขข้อมูลเรียบร้อยแล้ว')", true);
                                        GridView1.DataBind();
                                        GridView1.SelectedIndex = -1;
                                        Button3.Enabled = true;
                                        Button7.Enabled = false;
                                        clear();
                                        DataFamily();
                                    }
                                }
                            
                        }
                       
                        break;

                }
            }
    
          


        }
      
    

       
        

       
        protected void Button7_Click(object sender, EventArgs e)
        {
           

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            Button3.Enabled = false;
            Button7.Enabled = true;

            using (essystemEntities context = new essystemEntities())
            {

                List<family_mast> fma = context.family_mast.Where("it.[family_id]=" + ((HiddenField)GridView1.SelectedRow.Cells[1].FindControl("HiddenField1")).Value.ToString() + "").ToList();
                if (fma.Count > 0)
                {
                    List<family_mast> fm = context.family_mast.Where("it.[family_id]=" + ((HiddenField)GridView1.SelectedRow.Cells[1].FindControl("HiddenField1")).Value.ToString() + "").ToList();
                    if (fm.Count > 0)
                    {
                        TextBox4.Text = fm[0].family_code;
                        TextBox5.Text = fm[0].family_name;
                        family_id.Text = fm[0].family_id.ToString();
                        TextBox6.Text = fm[0].father_name;
                        TextBox10.Text = fm[0].mother_name;
                        TextBox8.Text = fm[0].email_father;
                        TextBox7.Text = fm[0].phone_father;
                        TextBox12.Text = fm[0].email_mother;
                        TextBox11.Text = fm[0].phone_mother;
                        TextBox3.Text = fm[0].receipt_name;
                        //TextBox13.Text =fm[0].address;
                        TextArea2.Value = fm[0].address;
                        TextBox14.Text = fm[0].parent_name;
                        TextBox15.Text = fm[0].phone_parent;
                       TextBox16.Text = fm[0].email_parent; 
                        
                    }
                }

            }
           
          
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            clear();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // loop all data rows
                foreach (DataControlFieldCell cell in e.Row.Cells)
                {
                    // check all cells in one row
                    foreach (Control control in cell.Controls)
                    {
                        // Must use LinkButton here instead of ImageButton
                        // if you are having Links (not images) as the command button.
                        ImageButton button = control as ImageButton;
                        if (button != null && button.CommandName == "Delete")
                            // Add delete confirmation
                            button.OnClientClick = "if (!confirm('คุณต้องการที่จะลบข้อมูล?')) return;";
                    }
                }
            }
        }


        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
           
            ///GridFO1.DataBind();
            GridView1.PageIndex = e.NewPageIndex;
            DataFamily();
            
            GridView1.DataBind();
          
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;
            strSQL = "DELETE FROM family_mast WHERE family_id ='" + GridView1.DataKeys[e.RowIndex].Value + "'";
            Conn.Open();
            objCmd = new MySqlCommand(strSQL, Conn);

            objCmd.ExecuteNonQuery();



            GridView1.EditIndex = -1;
            DataFamily();
        }



        
    }
}